package edu.service.impl;

import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import edu.common.Verification;
import edu.enumeration.RedisPathEnum;
import edu.service.UserService;
import edu.utils.RedisUtil;
import edu.utils.SMSUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 关于处理用户请求的 Service 层代码
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    RedisUtil redisUtil;

    @Override
    public Verification sendVerificationCode(String phone) {
        //获得随机数
        String verificationCode = SMSUtil.createRandomVerificationCode();
        if (redisUtil.hasKey(RedisPathEnum.VERIFICATION.getPath() + phone)) {
            //缓存中存在验证码
            return new Verification(202,redisUtil.getExpire(RedisPathEnum.VERIFICATION.getPath() + phone));
        }
        SendStatus sendStatus = SMSUtil.sendVerificationCode(phone,verificationCode);
        if (sendStatus == null) {
            return new Verification(201,null);
        }else if ("Ok".equals(sendStatus.getCode())) {
            redisUtil.set(RedisPathEnum.VERIFICATION.getPath() + phone,verificationCode,2);
            return new Verification(200,null);
        }
        return null;
    }

    @Override
    public Boolean doLogin(String phone, String verificationCode) {
        return null;
    }
}
